package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbp.dao.WalletRecordDao;
import com.jbp.model.PrizePoolEntity;
import com.jbp.model.User;
import com.jbp.model.WalletRecord;
import com.jbp.service.UserLevelService;
import com.jbp.service.UserService;
import com.jbp.service.WalletRecordService;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 钱包变动记录(WalletRecord)表服务实现类
 * @author cc
 * @since 2020-06-20 09:43:00
 */
@Service
public class WalletRecordServiceImpl extends ServiceImpl<WalletRecordDao, WalletRecord> implements WalletRecordService {
    @Autowired
    private UserService userService;
    @Autowired
    private UserLevelService userLevelService;

    @Override
    public PageUtils queryPage(Integer userId, Map<String, Object> params) {
        QueryWrapper<WalletRecord> queryWrapper=new QueryWrapper<>();
        if (params.get("coinType") != null && !params.get("coinType").equals("")){
            queryWrapper.like("coin_type",params.get("coinType"));
        }
        if (params.get("time") != null && !params.get("time").equals("")){
//            queryWrapper.like("create_time",params.get("time"));
            queryWrapper.eq("DATE_FORMAT(create_time, '%Y-%m' ) ",params.get("time"));
        }
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByDesc("create_time");

        IPage<WalletRecord> page = this.page(
                new Query<WalletRecord>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<WalletRecord> regalList(Map<String, Object> params) {
        return baseMapper.regalList(params);
    }

    @Override
    public List<WalletRecord> hd5() {
        List<WalletRecord> walletRecords = baseMapper.hd5();
        walletRecords.forEach(list -> {
            User user = userService.getById(list.getUserId());
            list.setUserName(user.getName());
            list.setUserImg(user.getImg());
            list.setUserSex(user.getSex());
            list.setUserLevel(user.getLevel());
            list.setUserLevelOn(userLevelService.levelOn(user.getId()));
        });
        return walletRecords;
    }

    @Override
    public List<PrizePoolEntity> prizePoolRank(Map<String, Object> params) {
        return baseMapper.prizePoolRank(params);
    }

    @Override
    public BigDecimal getChangeBalance(Integer userId) {
        return baseMapper.getChangeBalance(userId);
    }
}
